SHELL := /bin/bash

TOOLCHAIN_DIR := $(shell echo $${TOOLCHAIN_DIR:-/usr/local/k1tools/})
# Compilers
K1CC            ?= $(TOOLCHAIN_DIR)/bin/k1-gcc
CREATE_MULTIBIN ?= $(TOOLCHAIN_DIR)/bin/k1-create-multibinary

IO_APPLI_NAME := test_io
CLUSTER_APPLI_NAME := cluster
multibin-name := test.mpk

all:    mppa

mppa: clean
	${K1CC} -g -mos=nodeos cluster.c -o ${CLUSTER_APPLI_NAME} -lmppaipc
	${K1CC} -g -mos=rtems test_io.c -o ${IO_APPLI_NAME} -lmppaipc
	${CREATE_MULTIBIN} --toolchain ${TOOLCHAIN_DIR} --clusters ${CLUSTER_APPLI_NAME} --boot=${IO_APPLI_NAME} -T ${multibin-name}

traces: clean
	${K1CC} -g -DMPPA_TRACE_ENABLE -mos=nodeos cluster.c -o ${CLUSTER_APPLI_NAME} -lmppaipc
	${K1CC} -g -DMPPA_TRACE_ENABLE -mos=rtems test_io.c -o ${IO_APPLI_NAME} -lmppaipc
	${CREATE_MULTIBIN} --toolchain ${TOOLCHAIN_DIR} --clusters ${CLUSTER_APPLI_NAME} --boot=${IO_APPLI_NAME} -T ${multibin-name}

.PHONY: run-sim
run-sim:
	${TOOLCHAIN_DIR}/bin/k1-mppa -- ./${multibin-name}

run-sim-traces:
	${TOOLCHAIN_DIR}/bin/k1-mppa -p -- ./${multibin-name}

.PHONY: run-mppa
run-mppa:
	${TOOLCHAIN_DIR}/bin/k1-jtag-runner --multibinary ${multibin-name} --exec-multibin=IODDR0:${IO_APPLI_NAME}

.PHONY: run-dtv
run-dtv:
	${TOOLCHAIN_DIR}/bin/k1-mppa -p -- ./${multibin-name}
	
.PHONY: clean-traces
clean-traces:
	rm -rf MPPA.*.DSU.*

.PHONY: clean
clean:
	rm -rf ${IO_APPLI_NAME} ${CLUSTER_APPLI_NAME} ${multibin-name}
